package com.one.blocks.rbac.controller.convert.worker;

import com.one.blocks.rbac.constant.RbacConstant;
import com.one.blocks.rbac.controller.vo.SysOrgVo;
import com.one.blocks.rbac.domain.SysOrg;
import com.one.blocks.rbac.manager.SysOrgManager;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author linfuxin Created on 2022-05-05 15:37:25
 */
@Component
@Named("sysOrgQualifier")
@Mapper
public class SysOrgQualifier {

    @Autowired
    private SysOrgManager sysOrgManager;

    @AfterMapping
    @Named("processByManager")
    public void processByManager(SysOrgVo sysOrgVo, @MappingTarget SysOrg sysOrg) {
        SysOrg parentOrg = sysOrgManager.get(sysOrgVo.getParentId());
        sysOrg.setLevelNum(parentOrg == null ? RbacConstant.ROOT_LEVEL : parentOrg.getLevelNum() + 1);
        sysOrg.setFullCode(parentOrg == null ? sysOrgVo.getCode() : parentOrg.getFullCode() + "@" + sysOrgVo.getCode());
    }
}
